// pages/hotel/hotelDetail/book/occupantSelect/occupantSelect.js
Page({
  data: {

    checked: false,
    // user:[],
    guests: [],
    id: 0
  },
  getOccupant(options) {
    wx.request({
      url: 'http://localhost:8080/hotel/occupants/list',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp)
        this.setData({
          guests: resp.data.rows
        })
        // console.log(resp.data.rows)
      }
    })
  },
  onLoad(options) {
    this.getOccupant(options);
    this.goEdit(options);
    // const storedGuests = wx.getStorageSync('guests') || []; // 获取存储的客人信息
    // const allGuests = storedGuests.map(guest => ({...guest, checked: false}));
    // allGuests.forEach(element => {
    //   let identificationNumber = element.identificationNumber;
    //   const first7 = identificationNumber.slice(0, 7);
    //   const last4 = identificationNumber.slice(-4);
    //   identificationNumber = `${first7}****${last4}`;
    //   element.identificationNumbers = identificationNumber;
    // });
    // // console.log(222,allGuests)
    // this.setData({ guests: allGuests });
  },
  onChange(event) {
    const index = event.currentTarget.dataset.index;
    const guests = this.data.guests;
    guests[index].checked = !guests[index].checked;
    this.setData({
      guests,
      id: guests[index].id
    });
    console.log("index", index)
    console.log("guests", guests)
    console.log("guests[index]", guests[index])
  },
  goEdit(options) {
    // console.log(options)
    let selectedGuest = this.data.guests.find(guest => guest.checked === true);
    // console.log('selectedGuest',selectedGuest)
    if (selectedGuest) {
      wx.setStorageSync('selectedGuest', selectedGuest); // 将选定的客人信息保存到本地存储
      wx.navigateTo({
        url: '/pages/hotel/hotelDetail/book/editUserInfo/editUserInfo?id=' + this.data.id,
        // url: `/pages/hotel/hotelDetail/book/editUserInfo/editUserInfo?realName=${encodeURIComponent(selectedGuest.occupantsName)}&idCard=${encodeURIComponent(selectedGuest.identificationNumber)}&phone=${encodeURIComponent(selectedGuest.occupantsPhone)}`,
      });
    } else {
      wx.showToast({
        title: '请选择一位客人',
        icon: 'none',
        duration: 2000
      });
    }
  },
  addHandle: function () {
    wx.navigateTo({
      url: '/pages/hotel/hotelDetail/book/addOccupant/addOccupant',
    })
  },
  toSave(e) {

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      pageData: this.data
    });

    wx.navigateBack({
      delta: 1,  // 返回上一级页面
    })
    // wx.navigateTo({
    //   url: '/pages/hotel/hotelDetail/book/book?id='+this.data.id
    // })


  },


  // updateGuest(realName, cardType, idCard, phone) {
  //   const newGuest = { id: Date.now(), name: realName, cardType, idCard, phone };
  //   const guests = this.data.guests;
  //   guests.push(newGuest);
  //   this.setData({ guests });
  // },

  onShow() {
    // const query = wx.getStorageSync('query'); // 获取存储的查询参数
    // if (query && query.name && query.idCard && query.cardType)  {
    //   this.updateGuest(query.name, query.idCard,query.cardType);
    //   wx.removeStorageSync('query'); // 清除存储的查询参数
    // };
  },
});
